import 'dart:math';

import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_study/model/recommend_model.dart';
import 'package:flutter_study/repository/recommend_repository.dart';
import 'package:loading_more_list/loading_more_list.dart';

import 'recommend_widget_item.dart';

class RecommendPage extends StatefulWidget {
  @override
  _RecommendPageState createState() => _RecommendPageState();
}

class _RecommendPageState extends State<RecommendPage>
    with AutomaticKeepAliveClientMixin {
  var _recommendRepository = RecommendRepository();

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      body: RefreshIndicator(
        onRefresh: _refresh,
        child: LayoutBuilder(
          builder: (context, data) {
            final int crossAxisCount =
                max(data.maxWidth ~/ (ScreenUtil.getScreenW(context) / 2.0), 2);
            return LoadingMoreList<RecommendItem>(ListConfig<RecommendItem>(
                extendedListDelegate:
                    SliverWaterfallFlowDelegateWithFixedCrossAxisCount(
                  crossAxisCount: crossAxisCount,
                  crossAxisSpacing: 5,
                  mainAxisSpacing: 5,
                ),
                sourceList: _recommendRepository,
                padding: const EdgeInsets.all(5),
                lastChildLayoutType: LastChildLayoutType.foot,
                itemBuilder: (context, item, index) {
                  return RecommendWidgetItem(item: item);
                }));
          },
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;

  Future<void> _refresh() {
    return Future.value(Null);
  }
}
